package bizapi.sinosoft.com.provider.address;

import bizapi.sinosoft.com.provider.CommonResponse;
import com.weibo.api.motan.config.springsupport.annotation.MotanReferer;
import jd.api.request.address.AddressEntity;
import jd.api.service.address.JdAddressApi;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * Created by admin on 2017/2/9.
 */
@RestController
@RequestMapping("api/area")
public class AddressRestController {
    @MotanReferer
    JdAddressApi jdAddressApi;

    @RequestMapping(value = "getProvince", method = RequestMethod.POST)
    public CommonResponse<Map<String, Integer>> getProvince() {
        CommonResponse<Map<String, Integer>> commonResponse = new CommonResponse();
        try {
            commonResponse.setResult(jdAddressApi.getLevelOneAddress());
            commonResponse.setSuccess(true);
            return commonResponse;

        } catch (Exception e) {
            commonResponse.setResultMessage(e.getMessage());
            commonResponse.setSuccess(false);
            return commonResponse;
        }
    }

    /**
     * 获取京东二级地址
     */
    @RequestMapping(value = "getCity", method = RequestMethod.POST)
    public CommonResponse<Map<String, Integer>> getCity(Integer id) {
        CommonResponse<Map<String, Integer>> commonResponse = new CommonResponse();
        try {
            AddressEntity addressEntity = new AddressEntity();
            addressEntity.setId(id);
            commonResponse.setResult(jdAddressApi.getLevelTwoAddress(addressEntity));
            commonResponse.setSuccess(true);
            return commonResponse;
        } catch (Exception e) {
            commonResponse.setResultMessage(e.getMessage());
            commonResponse.setSuccess(false);
            return commonResponse;
        }
    }

    /**
     * 获取京东三级地址
     */
    @RequestMapping(value = "getCounty", method = RequestMethod.POST)
    public CommonResponse<Map<String, Integer>> getCounty(Integer id) {
        CommonResponse<Map<String, Integer>> commonResponse = new CommonResponse();
        try {
            AddressEntity addressEntity = new AddressEntity();
            addressEntity.setId(id);
            commonResponse.setResult(jdAddressApi.getLevelThreeAddress(addressEntity));
            commonResponse.setSuccess(true);
            return commonResponse;
        } catch (Exception e) {
            commonResponse.setResultMessage(e.getMessage());
            commonResponse.setSuccess(false);
            return commonResponse;
        }
    }

    /**
     * 获取京东四级地址
     */
    @RequestMapping(value = "getTown", method = RequestMethod.POST)
    public CommonResponse<Map<String, Integer>> getTown(Integer id) {
        CommonResponse<Map<String, Integer>> commonResponse = new CommonResponse();
        try {
            AddressEntity addressEntity = new AddressEntity();
            addressEntity.setId(id);
            commonResponse.setResult(jdAddressApi.getLevelFourAddress(addressEntity));
            commonResponse.setSuccess(true);
            return commonResponse;
        } catch (Exception e) {
            commonResponse.setResultMessage(e.getMessage());
            commonResponse.setSuccess(false);
            return commonResponse;
        }
    }
}

